package com.zhentao.controller;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Set;

@RestController
public class TestController {
    @Resource
    private RedisTemplate<String,Object> redisTemplate;


    @GetMapping("test")
    public void testaddList(){
        //在list左侧插入数据
        Long list1 = redisTemplate.opsForList().leftPushAll("list1", "11111", "2222222", "333333");
        System.err.println("插入长度"+list1);
        //从list右侧获取数据
        String list11 = (String) redisTemplate.opsForList().rightPop("list1");
        String list12 = (String) redisTemplate.opsForList().leftPop("list1");
        System.out.println("从右侧取"+list11);
        System.out.println("从左侧取"+list12);
    }
    @GetMapping("test1")
    public void testZset(){
        //添加数据到zset中  分值
        redisTemplate.opsForZSet().add("zset_key_001", "111111", 1);
        redisTemplate.opsForZSet().add("zset_key_001", "222222", 2);
        redisTemplate.opsForZSet().add("zset_key_001", "333333", 3);
        redisTemplate.opsForZSet().add("zset_key_001", "444444", 4);
        //按照分值获取数据
        Set<Object> zsetKey001 = redisTemplate.opsForZSet().rangeByScore("zset_key_001", 0, 1);
        Set<Object> zsetKey0011 = redisTemplate.opsForZSet().rangeByScore("zset_key_001", 0, 2);
        Set<Object> zsetKey0012 = redisTemplate.opsForZSet().rangeByScore("zset_key_001", 0, 3);
        Set<Object> zsetKey0013 = redisTemplate.opsForZSet().rangeByScore("zset_key_001", 0, 4);
        Set<Object> zsetKey0014 = redisTemplate.opsForZSet().rangeByScore("zset_key_001", 1, 4);
        System.err.println(zsetKey001);
        System.err.println(zsetKey0011);
        System.err.println(zsetKey0012);
        System.err.println(zsetKey0013);
        System.err.println(zsetKey0014);
    }
}
